//------------------------------------------------------------------------------
#include <QVariant>
#include <QSqlRecord>
#include "CFindResultFrm.h"
#include "CFindAvanceFrm.h"
#include "CFindFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CFindFrm::CFindFrm(QString table, QWidget *parent, QString requete,  QSize size, QPoint point, int colSort, Qt::SortOrder order, int goToLigne) : QDialog(parent)
{
	setupUi(this);
	_table=table;
	_result="";
	_requete="";
	_requeteHum="";
	sizeFrmResult=size;
	posFrmResult=point;
	_colSort=colSort;
	_sortOrder=order;
	_goToLigne=goToLigne;
	
	listChamp=new QStringList();
	header=new QStringList();
	
	loadChamps(false);
	
	if(requete == "")
	{	
		cbOperation->addItem(tr("Contient"), QVariant("like \"\%%1\%\""));
		cbOperation->addItem(tr("Commence par"), QVariant("like \"%1\%\""));
		cbOperation->addItem(tr("Se termine par"), QVariant("like \"\%%1\""));
		cbOperation->addItem(tr("Est égal à"), QVariant(" = \"%1\""));
		cbOperation->addItem(tr("Est différent de"), QVariant(" <> \"%1\""));
		cbOperation->addItem(tr("Est supérieur à"), QVariant(" > \"%1\""));
		cbOperation->addItem(tr("Est inférieur à"), QVariant(" < \"%1\""));
		
		lineEditValeur->setFocus();
	}else
	{
		executeRequete(requete);
	}
}
//------------------------------------------------------------------------------
void CFindFrm::loadChamps(bool liaisons)
{
	int defaut=0;
	int idx=0;
	QVector<CMySQL::SChampRecherche> vector=CMySQL::getInstance()->getChampsRecherche(_table, liaisons);
	QVector<CMySQL::SChampRecherche>::iterator i;
	
	if(liaisons)
	{
		QString requete="SELECT tableDest FROM rechercheLier WHERE tableSource=\""+_table+"\"";
		QSqlQuery *query;
		
		if(CMySQL::getInstance()->execQuery(requete, &query))
		{
			while(query->next())
			{
				vector = vector << CMySQL::getInstance()->getChampsRecherche(query->record().value("tableDest").toString(), true);
			}
		}
		delete query;
	}
	
	i=vector.begin();
	while(i != vector.end())
	{
		cbChamp->addItem((*i).champDescription, QVariant((*i).champNom));
		listChamp->append((*i).champNom);
		header->append((*i).champDescription);
		if((*i).defaut) defaut=idx;
		i++;
		idx++;
	}	
	cbChamp->setCurrentIndex(defaut);
}
//------------------------------------------------------------------------------
void CFindFrm::executeRequete(QString requete)
{
	QSqlQuery *query;
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		switch(query->size())
		{
			case 0:
				_result="";
				setResult(QDialog::Rejected);
				hide();
				break;
			case 1:
				query->next();
				_result=query->record().value(0).toString();
				setResult(QDialog::Accepted);
				hide();
				break;
			default:
				CFindResultFrm *findResultFrm=new CFindResultFrm(this, query, header, sizeFrmResult, posFrmResult, _colSort,_sortOrder, _goToLigne);
				QDialog::DialogCode ret;
				
				findResultFrm->exec();
				ret=(QDialog::DialogCode)findResultFrm->result();
				if(ret == QDialog::Accepted)
				{
					_result=findResultFrm->getResult();
				}
				
				_colSort=findResultFrm->getColSort();
				_sortOrder=findResultFrm->getSortOrder();
				sizeFrmResult=findResultFrm->size();
				posFrmResult=findResultFrm->pos();
				_goToLigne=findResultFrm->getLastLigne();
				
				delete findResultFrm;
				setResult(ret);
				hide();
				break;
		}
	}
	
	delete query;
}
//------------------------------------------------------------------------------
void CFindFrm::accept(void)
{
	QString champs="";
	QString group="";
	QString s="";
	int i;
	
	_requete="SELECT ";
	
	for(i=0;i<listChamp->size();i++)
	{
		champs+=s+listChamp->at(i);
		s=", ";
	}
	
	_requete+=champs+" FROM "+_table+" ";
	
	if(chkLiaison->checkState() == Qt::Checked)
	{
		QString requete="SELECT champSource, tableDest, champDest FROM rechercheLier WHERE tableSource=\""+_table+"\"";
		QSqlQuery *query;
		
		if(CMySQL::getInstance()->execQuery(requete, &query))
		{
			while(query->next())
			{
				QSqlRecord rec=query->record();
				
				_requete+=" INNER JOIN "+rec.value("tableDest").toString();
				_requete+=" ON "+_table+"."+rec.value("champSource").toString()+"="+rec.value("tableDest").toString()+"."+rec.value("champDest").toString();
			}
		}
		delete query;
		
		group=" GROUP BY "+listChamp->at(0);
	}
	
	_requete+=" WHERE "+cbChamp->itemData(cbChamp->currentIndex()).toString();
	_requete+=" "+cbOperation->itemData(cbOperation->currentIndex()).toString().arg(lineEditValeur->text());
	_requete+=group;
	
	_requeteHum=cbChamp->currentText()+" "+cbOperation->currentText()+" '"+lineEditValeur->text()+"'";
	
	executeRequete(_requete);
}
//------------------------------------------------------------------------------
void CFindFrm::on_boutonRechercheAvance_clicked(bool checked)
{
	CFindAvanceFrm * findAvanceFrm=new CFindAvanceFrm(_table, chkLiaison->checkState() == Qt::Checked, this);
	
	findAvanceFrm->setModal(true);
	findAvanceFrm->exec();
	if(findAvanceFrm->result() == QDialog::Accepted)
	{
		_requete=findAvanceFrm->getRequete();
		_requeteHum=findAvanceFrm->getRequeteHum();
		header=findAvanceFrm->getHeader();
		
		executeRequete(_requete);
	}
}
//------------------------------------------------------------------------------
void CFindFrm::on_chkLiaison_clicked(bool checked)
{
	while(cbChamp->count())
	{
		cbChamp->removeItem(0);	
	}
	listChamp->clear();
	
	loadChamps(checked);
}
//------------------------------------------------------------------------------